package com.jhgsys.internal.hazard.service.impl;

import com.jhgsys.internal.hazard.entity.HazardReportConfig;
import com.jhgsys.internal.hazard.mapper.HazardReportConfigMapper;
import com.jhgsys.internal.hazard.service.IHazardReportConfigService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author Lujiachen
 * @date 2024-08-12 15:42:05
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardReportConfigServiceImpl extends ServiceImpl<HazardReportConfigMapper, HazardReportConfig> implements IHazardReportConfigService {

    /**
     * 查询（分页）
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     * @param request QueryRequest
     * @param hazardReportConfig hazardReportConfig
     * @return IPage<HazardReportConfig>
    */
    @Override
    public IPage<HazardReportConfig> findHazardReportConfigPage(QueryRequest request, HazardReportConfig hazardReportConfig) {
        LambdaQueryWrapper<HazardReportConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportConfig::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardReportConfig> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     * @param hazardReportConfig hazardReportConfig
     * @return List<HazardReportConfig>
     */
    @Override
    public List<HazardReportConfig> findHazardReportConfigList(HazardReportConfig hazardReportConfig) {
	    LambdaQueryWrapper<HazardReportConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportConfig::getDeletedFlg, 0);
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author Lujiachen
    * @date 2024-08-12 15:42:05
    * @param id
    * @return HazardReportConfig
    */
    @Override
    public HazardReportConfig findHazardReportConfigById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 根据所属公司查询
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     * @param tenantId
     * @return HazardReportConfig
     */
    @Override
    public HazardReportConfig findHazardReportConfigByTenantId(String tenantId){
        LambdaQueryWrapper<HazardReportConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardReportConfig::getDeletedFlg, 0);
        queryWrapper.eq(HazardReportConfig::getTenantId, tenantId);
        queryWrapper.last(" LIMIT 1 ");
        return this.baseMapper.selectOne(queryWrapper);
    }

    /**
     * 新增
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     * @param hazardReportConfig hazardReportConfig
     */
    @Override
    @Transactional
    public void createHazardReportConfig(HazardReportConfig hazardReportConfig) {
        hazardReportConfig.setCreatedTime(new Date());
        this.save(hazardReportConfig);
    }

    /**
     * 修改
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     * @param hazardReportConfig hazardReportConfig
     */
    @Override
    @Transactional
    public void updateHazardReportConfig(HazardReportConfig hazardReportConfig) {
        hazardReportConfig.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardReportConfig);
    }

    /**
     * 逻辑删除
     * @author Lujiachen
     * @date 2024-08-12 15:42:05
     * @param hazardReportConfig hazardReportConfig
     * @param hazardReportConfigIds hazardReportConfigIds
     */
    @Override
    @Transactional
    public void deleteHazardReportConfig(HazardReportConfig hazardReportConfig,String hazardReportConfigIds) {
        List<String> list = Arrays.asList(hazardReportConfigIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardReportConfig,new LambdaQueryWrapper<HazardReportConfig>().in(HazardReportConfig::getReportConfigId,list));
	}

    /**
    * 物理删除
    * @author Lujiachen
    * @date 2024-08-12 15:42:05
    * @param hazardReportConfigIds hazardReportConfigIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardReportConfig(String hazardReportConfigIds) {
        List<String> list = Arrays.asList(hazardReportConfigIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardReportConfig> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardReportConfig::getReportConfigId,list);
        this.remove(wapper);
    }
}
